package seunit.framework.automation;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

/**
 * @class actions
 * actions.java
 * Purpose: Actions class provides diffrent Actions in Selenium.
 *
 * @author Vishal Sangave
 * @version 1.0 12 June 2012
 */
public class actions {
	WebDriver driver;
	WebElement element;
	locator location;
	
	/**
	 * actions
	 * 
     * Constructor of action which requires the driver as parameter
     * Whenever Instantiate the code pass driver
     * 
     * @author Vishal Sangave
     * @param Webdriver driver
     * 
     */
	
	public actions(WebDriver _driver){
		this.driver = _driver;
		location = new locator(driver);
	}
	
	/**
	 * selectComboValue
	 * 
     * The selectComboValue method Selects the option from drop down.
     * Selects by index, value and label.
     * Need to provide Specifier in value example "index=2", "value=abc" or "label=name"
     * by default is the Specifier is not given it will take the string as label.
     * 
     * @author Vishal Sangave
     * @param String elementId 
     * @param String value
     * @return null
     */
	public void selectComboValue(final String elementId, final String value) {
	    final Select selectBox;
	    element = location.getElement(elementId);
	    selectBox = new Select(element);
	    if(value.startsWith("index="))
	    {
	    	selectBox.selectByIndex(Integer.parseInt(value.replace("index=", "")));
	    	
	    }
	    else if(value.startsWith("value="))
	    {
	    	selectBox.selectByValue(value.replace("value=", ""));
	    }
	    else if(value.startsWith("label="))
	    {
	    	selectBox.selectByVisibleText(value.replace("label=", ""));
	    }
	    else
	    {
	    	selectBox.selectByVisibleText(value);
	    }
	}
	
	
	/**
	 * getEval
	 * 
     * The getEval method Evaluates/Executes the JavaScript specidied and returns the output string.
     * example 
     * 
     * @author Vishal Sangave
     * @param String script 
     * @return String
     */
	public String getEval(String script){
		return (String) ((JavascriptExecutor)driver).executeScript("");
	}
	
	public void upload(String locator,String path ){
	WebElement fileUpload = location.getElement(locator);

		
	}
	
	public void hiddenUpload(String locator,String path){
		WebElement fileUpload = location.getElement(locator);
		((JavascriptExecutor)driver).executeScript("arguments[0].style.visibility = 'visible'; arguments[0].style.height = '1px'; arguments[0].style.width = '1px'; arguments[0].style.opacity = 1", fileUpload);
	}
}
